/**
 * Project Name:HomeActivity
 * Date:2016-3-22下午7:27:17
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.view;


import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ProgressBar;

/**
 * ClassName: RiseProgressBar <br/>
 * date: 2016-3-22 下午7:27:17 <br/>
 *
 * @author askzhao
 * @version 
 */
public class RiseProgressBar extends RoundCornerProgressBar{

	private float fromProgress;
	private float progress;
	private static final int STOPPED = 0;

	private static final int RUNNING = 1;

	private int mPlayingState = STOPPED;

	private ValueAnimator valueAnimator;
	private long duration = 1500;

	private EndListener mEndListener = null;

	final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
			99999999, 999999999, Integer.MAX_VALUE };

	public RiseProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public RiseProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	
	
	public interface EndListener {
		public void onEndFinish();
	}

	public boolean isRunning() {
		return (mPlayingState == RUNNING);
	}


	private void runInt() {
		valueAnimator = ValueAnimator.ofInt((int) fromProgress,
				(int) progress);
		valueAnimator.setDuration(duration);

		valueAnimator
				.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
					@Override
					public void onAnimationUpdate(ValueAnimator valueAnimator) {

						setProgress((Integer) valueAnimator.getAnimatedValue());
//						System.out.println("闯哥  valueAnimator.getAnimatedValue()= "+valueAnimator.getAnimatedValue());
						if (valueAnimator.getAnimatedFraction() >= 1) {
							mPlayingState = STOPPED;
//							System.out.println("闯哥  valueAnimator.getAnimatedFraction() >= 1");
							if (mEndListener != null)
								mEndListener.onEndFinish();
						}
					}
				});
		valueAnimator.start();
		valueAnimator.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
				// TODO Auto-generated method stub
				fromProgress = progress;
//				System.out.println("闯哥  其实我结束了");
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	static int sizeOfInt(int x) {
		for (int i = 0;; i++)
			if (x <= sizeTable[i])
				return i + 1;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
	}


	public void start() {

		if (!isRunning()) {
			mPlayingState = RUNNING;
			runInt();
		}
	}


	public RiseProgressBar withNumber(int number) {
		this.progress = number;
		return this;

	}


	public RiseProgressBar setDuration(long duration) {
		this.duration = duration;
		return this;
	}


	public void setOnEnd(EndListener callback) {
		mEndListener = callback;
	}

	/**
	 * init:(这里用一句话描述这个方法的作用). <br/>
	 * @author askzhao
	 */
	public void init() {
		fromProgress = 0;
		progress = 0;
		mPlayingState = STOPPED;
		if(valueAnimator!=null){
			valueAnimator.cancel();
			valueAnimator = null;
		}
		
	}

	
	

}
